<?php

namespace App\Http\Controllers\Admin;

use App\Models\CommissionRecords;
use App\Models\CommissionWithdraw;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CommissionController extends BaseController
{
    public function records(Request $request)
    {
        $items = CommissionRecords::with('user')->orderByDesc('id')->paginate(20);
        return $this->view('admin.commission.records',[
            'items'=>$items,
            'pagination'=>$items->render()
        ]);
    }

    public function delRecords(Request $request)
    {
        $delete = $request->input('delete');
        if ($delete)
        {
            foreach ($delete as $id)
            {
                CommissionRecords::where('id', $id)->delete();
            }
        }
        return ajaxReturn();
    }

    public function withdraw(Request $request)
    {

        $items = CommissionWithdraw::with(['card', 'user'])->orderByDesc('id')->paginate(20);
        return $this->view('admin.commission.withdraw',[
            'items'=>$items,
            'pagination'=>$items->render()
        ]);
    }

    public function deleteWithdraw(Request $request)
    {
        $delete = $request->input('delete');
        foreach ($delete as $id)
        {
            CommissionWithdraw::where('id', $id)->delete();
        }
        return ajaxReturn();
    }

    public function showWithdraw(Request $request)
    {
        $id = $request->input('id');
        $withdraw = CommissionWithdraw::with(['card', 'user'])->find($id);

        return $this->view('admin.commission.showwithdraw', compact('withdraw', 'id'));
    }

    public function resolveWithdraw(Request $request)
    {
        $id = $request->input('id');
        CommissionWithdraw::find($id)->update(['status'=>1]);
        return ajaxReturn();
    }

    public function rejectWithdraw(Request $request)
    {
        $id = $request->input('id');
        CommissionWithdraw::find($id)->update(['status'=>2]);
        return ajaxReturn();
    }
}
